package com.liuya.session.controller;

import com.liuya.base.BaseController;
import com.liuya.base.BaseService;
import com.liuya.common.WebUtil;
import com.liuya.page.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * session管理
 * @author liuya
 * @date 20160830
 */
@RestController
@Api(value = "会话管理", description = "会话管理")
@RequestMapping(value = "/session", method = RequestMethod.POST)
public class SysSessionController extends BaseController {

    @Autowired
    private BaseService sessionService;

    // 查询会话
    @ApiOperation(value = "查询会话")
    @RequiresPermissions("sys.session.list")
    @RequestMapping(value = "/list")
    public Object list(HttpServletRequest request, ModelMap modelMap) {
        Map<String, Object> params = WebUtil.getParameters(request);
        Pageable<?> list = sessionService.selectPage(params);
//        Long number = SessionListener.getAllUserNumber();
//        modelMap.put("userNumber", number); // 用户数大于会话数,有用户没有登录
        return setSuccessModelMap(modelMap, list);
    }

    // 删除会话
    @ApiOperation(value = "删除会话")
    @RequiresPermissions("sys.session.delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Object delete(ModelMap modelMap, @RequestParam(value = "id", required = false) String id) {
        sessionService.deleteById(id);
        return setSuccessModelMap(modelMap);
    }
}
